<template>
  <div class="reserve-main-layout">
    <!-- 顶部统计卡片 -->
    <div class="reserve-stats">
      <el-card class="stat-card" shadow="hover">
        <div class="stat-title">总预订数</div>
        <div class="stat-value">{{ total }}</div>
      </el-card>
      <el-card class="stat-card" shadow="hover">
        <div class="stat-title">本月预订</div>
        <div class="stat-value">{{ monthCount }}</div>
      </el-card>
      <el-card class="stat-card" shadow="hover">
        <div class="stat-title">本周预订</div>
        <div class="stat-value">{{ weekCount }}</div>
      </el-card>
    </div>
    <!-- 搜索与操作区 -->
    <div class="reserve-header">
      <el-input v-model="params.name" placeholder="搜索商品名称" class="search-input"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="findBySearchlive">查询</el-button>
      <el-button @click="resetlive">清空</el-button>
      <el-button type="success" icon="el-icon-plus" v-if="user.role === 'ROLE_ADMIN'" @click="add">新增</el-button>
    </div>
    <!-- 表格区 -->
    <el-card class="reserve-table-card">
      <el-table :data="tableData" stripe border>
        <el-table-column prop="liveName" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="userName" label="预订人" width="120"></el-table-column>
        <el-table-column prop="time" label="预定时间" width="180"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-popconfirm title="确认删除该记录？" @confirm="dellive(scope.row.id)">
              <el-button slot="reference" type="danger" size="mini" v-if="user.role === 'ROLE_ADMIN'">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="reserve-pagination"
        @size-change="handleSizeChangelive"
        @current-change="handleCurrentChangelive"
        :current-page="params.pageNum"
        :page-sizes="[5,10,15,20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 弹窗表单 -->
    <el-dialog title="新增预订" :visible.sync="dialogFormVisible" width="420px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.liveName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预订人">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预定时间">
          <el-date-picker
            v-model="form.time"
            type="datetime"
            placeholder="选择日期时间"
            style="width: 100%;"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitlive">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        name: '',
        pageNum: 1,
        pageSize: 8,
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {},
      monthCount: 0,
      weekCount: 0,
    }
  },
  created() {
    this.findBySearchlive();
    this.getStats();
  },
  methods: {
    findBySearchlive(){
      request.get("/reservelive/search", {
        params: this.params
      }).then(res =>{
        if(res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else{
          this.$message.error(res.msg);
        }
      })
    },
    getStats() {
      // 假设后端有统计接口，否则可在findBySearchlive后端返回统计
      request.get("/reservelive/stats").then(res => {
        if(res.code === '0'){
          this.monthCount = res.data.monthCount || 0;
          this.weekCount = res.data.weekCount || 0;
        }
      }).catch(()=>{});
    },
    add(){
      this.form = {};
      this.dialogFormVisible = true
    },
    resetlive(){
      this.params = {
        pageNum: 1,
        pageSize: 8,
        name: ''
      }
      this.findBySearchlive();
    },
    handleSizeChangelive(pageSize){
      this.params.pageSize = pageSize;
      this.findBySearchlive();
    },
    handleCurrentChangelive(pageNum){
      this.params.pageNum = pageNum;
      this.findBySearchlive();
    },
    submitlive(){
      request.post("/reservelive", this.form).then(res =>{
        if(res.code === "0"){
          this.$message.success("操作成功");
          this.dialogFormVisible = false;
          this.findBySearchlive();
          this.getStats();
        }else{
          this.$message.error(res.msg);
        }
      })
    },
    dellive(id){
      request.delete("/reservelive/" + id).then(res => {
        if (res.code === "0") {
          this.$message.success("删除成功");
          this.findBySearchlive();
          this.getStats();
        } else {
          this.$message.error(res.msg);
        }
      })
    }
  }
}
</script>

<style scoped>
.reserve-main-layout {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 12px 40px 12px;
  background: #f7fafd;
  min-height: 100vh;
}
.reserve-stats {
  display: flex;
  gap: 24px;
  margin-bottom: 28px;
}
.stat-card {
  flex: 1;
  text-align: center;
  border-radius: 12px;
  background: linear-gradient(135deg, #e3f0ff 0%, #f9f9f9 100%);
  box-shadow: 0 4px 18px rgba(64,158,255,0.07);
  padding: 18px 0 10px 0;
}
.stat-title {
  font-size: 15px;
  color: #888;
  margin-bottom: 8px;
}
.stat-value {
  font-size: 28px;
  color: #409EFF;
  font-weight: bold;
}
.reserve-header {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}
.search-input {
  width: 260px;
}
.reserve-table-card {
  border-radius: 14px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.07);
  background: #fff;
  margin-bottom: 24px;
  padding: 18px 18px 0 18px;
}
.el-table {
  border-radius: 8px;
  overflow: hidden;
}
.el-table__header th {
  background-color: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}
.el-table__body tr:hover td {
  background-color: #f5f7fa !important;
}
.reserve-pagination {
  margin-top: 20px;
  padding: 16px 0 16px 0;
  background: #fff;
  border-radius: 8px;
}
.dialog-footer {
  text-align: right;
  padding: 20px 0 0;
}
.el-form-item {
  margin-bottom: 18px;
}
@media (max-width: 900px) {
  .reserve-stats {
    flex-direction: column;
    gap: 12px;
  }
  .reserve-main-layout {
    padding: 8px 2px 20px 2px;
  }
  .reserve-table-card {
    padding: 6px 2px 0 2px;
  }
}
</style>